package com.example.chinawenddingexpo.controller;

import com.example.chinawenddingexpo.model.Event;
import com.example.chinawenddingexpo.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/event")
public class EventController {
    
    @Autowired
    private EventService eventService;
    
    // 获取所有可预约活动
    @GetMapping("/available")
    public Map<String, Object> getAvailableEvents() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Event> events = eventService.getAllAvailableEvents();
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", events);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取活动列表失败");
        }
        return result;
    }
    
    // 按类型获取活动
    @GetMapping("/type/{type}")
    public Map<String, Object> getEventsByType(@PathVariable Event.EventType type) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Event> events = eventService.getEventsByType(type);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", events);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取指定类型活动失败");
        }
        return result;
    }
    
    // 获取即将开始的活动
    @GetMapping("/upcoming")
    public Map<String, Object> getUpcomingEvents() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Event> events = eventService.getUpcomingEvents();
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", events);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取即将开始活动失败");
        }
        return result;
    }
    
    // 获取参展商的活动
    @GetMapping("/exhibitor/{exhibitorId}")
    public Map<String, Object> getEventsByExhibitor(@PathVariable Long exhibitorId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Event> events = eventService.getEventsByExhibitor(exhibitorId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", events);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取参展商活动失败");
        }
        return result;
    }
    
    // 获取活动详情
    @GetMapping("/{id}")
    public Map<String, Object> getEventDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Event event = eventService.getEventById(id);
            if (event != null) {
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", event);
            } else {
                result.put("code", 404);
                result.put("msg", "活动不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取活动详情失败");
        }
        return result;
    }
    
    // 保存活动
    @PostMapping
    public Map<String, Object> saveEvent(@RequestBody Event event) {
        Map<String, Object> result = new HashMap<>();
        try {
            Event savedEvent = eventService.saveEvent(event);
            result.put("code", 200);
            result.put("msg", "保存成功");
            result.put("data", savedEvent);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "保存活动失败");
        }
        return result;
    }
}